package com.dgut.admodule.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.admodule.dao.IAdsbidDao;
import com.dgut.admodule.dao.IBiddingDao;
import com.dgut.admodule.entity.Adsbid;
import com.dgut.admodule.entity.Bidding;
import com.dgut.admodule.entity.dto.AdsbidAddDTO;

@Service
@Transactional
public class AdsbidService implements IAdsbidService {

	@Autowired
	private IAdsbidDao adsbidDao;
	@Autowired
	private IBiddingDao biddingDao;
	
	public void saveOrUpdate(AdsbidAddDTO adsbidAddDTO) {
		int i=0;
		Bidding bidding = new Bidding();
		bidding = biddingDao.findOne(adsbidAddDTO.getBidId());
		Adsbid adsbid = new Adsbid();
		
		//如果没有adsbid，bidding才添加
		for(Adsbid a:bidding.getAdsbid()) {
			if(a.getId()==adsbidAddDTO.getId()) {
				i=1;
			}
		}
		if(i==0) {
			adsbid.setcName(adsbidAddDTO.getcName());
			adsbid.setBidPrice(adsbidAddDTO.getBidPrice());
			adsbid.setAdsName(adsbidAddDTO.getAdsName());
			adsbid.setAdsDecription(adsbidAddDTO.getAdsDecription());
			adsbid.setBidding(bidding);
			bidding.getAdsbid().add(adsbid);
		}else {
			adsbid = adsbidDao.findOne(adsbidAddDTO.getId());
			adsbid.setcName(adsbidAddDTO.getcName());
			adsbid.setBidPrice(adsbidAddDTO.getBidPrice());
			adsbid.setAdsName(adsbidAddDTO.getAdsName());
			adsbid.setAdsDecription(adsbidAddDTO.getAdsDecription());
		}
		
		adsbidDao.save(adsbid);
	}

	public void delete(Adsbid adsbid) {
		if (adsbid != null) {
			adsbid.setBidding(null);
			adsbidDao.delete(adsbid);
		}else {
			System.out.println("找不到对象！");
		}
	}

	public void delete(List<Long> ids) {
		List<Adsbid> adsbids =  (List<Adsbid>) adsbidDao.findAll(ids);
		if(adsbids!=null) {
			for(Adsbid a : adsbids)
			{
				a.setBidding(null);
			}
			adsbidDao.delete(adsbids);
		}
	}

	@Transactional(readOnly = true)
	public Adsbid findOne(Long id) {
		return adsbidDao.findOne(id);
	}

	@Transactional(readOnly = true)
	public List<Adsbid> findAll() {
		return (List<Adsbid>) adsbidDao.findAll();
	}

	@Transactional(readOnly = true)
	public Adsbid findCompany(String cName) {
		if(cName!=null)
		{
			Long aid = null;
			for(Adsbid a: adsbidDao.findCompany(cName))
			{
				aid = a.getId();
			}
			if(aid!=null) {
				return adsbidDao.findOne(aid);
			}else {
				return new Adsbid();
			}
		}else {
			return new Adsbid();
		}
	}
	
	@Transactional(readOnly = true)
	public Page<Adsbid> findAll(Specification<Adsbid> spec, Pageable pageable) {
		return adsbidDao.findAll(spec, pageable);
	}

	public List<Adsbid> findByBid(Long bidId) {
		return (List<Adsbid>) adsbidDao.findByBid(bidId);
	}

}
